package cn.bellychang.leetcode.question215;

import java.util.PriorityQueue;

/**
 * @author ChangLiang
 * @date 2021/3/17
 */
public class Solution3 {

    public int findKthLargest(int[] nums, int k) {

        // PriorityQueue默认就是一个小顶堆
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(nums.length, (o1, o2) -> {
            if (o1 > o2) {
                return -1;
            } else if (o1 < o2) {
                return 1;
            } else {
                return 0;
            }
        });
        for (int i = 0; i < nums.length; i++) {
            priorityQueue.add(nums[i]);
        }
        for (int i = 0; i < k-1; i++) {
            priorityQueue.poll();
        }
        return priorityQueue.peek();
    }
}
